/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;

import Merlion.FRM.bean.PaymentBeanRemote;
import Merlion.FRM.entity.RefundEntity;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;

@ManagedBean(name = "RefundManagedBean")
@ViewScoped
public class RefundManagedBean {

    private Long refundId;
    private Long salesOrderId;
    private Long customerId;
    private boolean refundStatus;
    private List<RefundEntity> selectedRefunds;
    private RefundEntity selectedViewRefund;
    private RefundEntity[] selectedRefundsd;
    
    @EJB
    private PaymentBeanRemote pbr;

    public RefundManagedBean() {
    }

    public void initView(PhaseEvent event) {
        System.out.println("**initView");
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        if (selectedRefunds == null) {
            System.out.println("**initView : viewInvoice() ");
            selectedRefunds = pbr.viewAllRefund();
            selectedRefunds.size();
        }
        if (selectedRefunds != null && selectedViewRefund == null) {
            selectedViewRefund = selectedRefunds.get(0);
        }
    }

    public void doRefundPayment(ActionEvent event) {
        System.out.println("**RefundManagedBean : doRefundPayment()");
        
        try {
            if (getSelectedViewRefund() != null) {
                refundId = getSelectedViewRefund().getRefundId();
                salesOrderId = getSelectedViewRefund().getSalesOrderId();
                customerId = getSelectedViewRefund().getCustomerId();
                refundStatus = pbr.refundCashInAdvance(customerId, salesOrderId);
            } else {
                System.out.println("**RefundManagedBean: Selected refund data is null");
            }
            FacesContext.getCurrentInstance().getExternalContext().redirect("ViewRefundEntity.xhtml");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Long getCustomerId() {
        return customerId;
    }

    public void setCustomerId(Long customerId) {
        this.customerId = customerId;
    }

    public PaymentBeanRemote getPbr() {
        return pbr;
    }

    public void setPbr(PaymentBeanRemote pbr) {
        this.pbr = pbr;
    }

    public Long getRefundId() {
        return refundId;
    }

    public void setRefundId(Long refundId) {
        this.refundId = refundId;
    }

    public boolean isRefundStatus() {
        return refundStatus;
    }

    public void setRefundStatus(boolean refundStatus) {
        this.refundStatus = refundStatus;
    }

    public Long getSalesOrderId() {
        return salesOrderId;
    }

    public void setSalesOrderId(Long salesOrderId) {
        this.salesOrderId = salesOrderId;
    }

    public List<RefundEntity> getSelectedRefunds() {
        return selectedRefunds;
    }

    public void setSelectedRefunds(List<RefundEntity> selectedRefunds) {
        this.selectedRefunds = selectedRefunds;
    }

    public RefundEntity[] getSelectedRefundsd() {
        return selectedRefundsd;
    }

    public void setSelectedRefundsd(RefundEntity[] selectedRefundsd) {
        this.selectedRefundsd = selectedRefundsd;
    }

    public RefundEntity getSelectedViewRefund() {
        return selectedViewRefund;
    }

    public void setSelectedViewRefund(RefundEntity selectedViewRefund) {
        this.selectedViewRefund = selectedViewRefund;
    }
    
}
